/*
 *  msn.c - MSN Messenger Functions
 *  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <string.h>

#include "notification.h"
#include "msn.h"
#include "utility.h"

int
get_client_caps (void)
{
    int msncx = 0;	/* Client version supported */
	int caps = 0;	/* Additional capabilities */

    msncx = CLIENT_MSNC1;
    caps += msncx;

    return caps;
}

int
msn_signout (MsnSession *session)
{
    g_return_val_if_fail (session != NULL, -1);
    g_return_val_if_fail (session->notif->connected, -1);

    msn_notification_sign_out (session->notif);
    msn_session_free (session);

    return 0;
}

int
msn_keepalive (MsnConnection *conn)
{
    int ret = 0;

    g_return_val_if_fail (conn != NULL, -1);
    g_return_val_if_fail (conn->socket->connected, -1);

    ret = send_png (conn);

    return ret;
}

int
msn_change_nickname (MsnSession *session, const char *nick)
{
    char *url_nick = NULL;
    int ret = 0;

    g_return_val_if_fail (session != NULL, -1);
    g_return_val_if_fail (nick != NULL, -1);
    g_return_val_if_fail (session->acct->passport != NULL, -1);
    g_return_val_if_fail (session->notif->connected, -1);

    url_nick = url_encode (nick);

    /* TODO: Input validation !! */
    ret = send_prp (session->notif->conn, url_nick);

    g_free (url_nick);

    return ret;
}

int
msn_set_status (MsnSession *session, MsnStatus status)
{
    char *str_status = NULL;
    int caps = 0;   /* Client Capabilities */
    int ret = 0;

    g_return_val_if_fail (session != NULL, -1);

    switch (status) {
        case ONLINE:
            str_status = "NLN";
        break;
        case BUSY:
            str_status = "BSY";
        break;
        case IDLE:
            str_status = "IDL";
        break;
        case BRB:
            str_status = "BRB";
        break;
        case AWAY:
            str_status = "AWY";
        break;
        case PHONE:
            str_status = "PHN";
        break;
        case LUNCH:
            str_status = "LUN";
        break;
        case HIDDEN:
            str_status = "HDN";
        break;
    }

    if (str_status == NULL)
        return -1;

    caps = get_client_caps ();

    ret = send_chg (session->notif->conn, str_status, caps);

    return ret;
}

int
msn_send_file (MsnSession *session)
{
    return -1;
}

int
msn_start_chat (MsnSession *session)
{
    return -1;
}

int
msn_add_contact (MsnSession *session, MsnContact *contact)
{
    return -1;
}

int
msn_remove_contact (MsnSession *session, MsnContact *contact)
{
    return -1;
}

int
msn_move_contact (MsnSession *session,
				  MsnGroup *from,
				  MsnGroup *to,
				  MsnContact *contact)
{
    return -1;
}

int 
msn_add_group (MsnSession *session, const char *group_name)
{
	char *encoded = NULL;
	int ret = 0;

	g_return_val_if_fail (session != NULL, -1);
	g_return_val_if_fail (group_name != NULL, -1);
	g_return_val_if_fail (strlen (group_name) > 0, -1);

	/* Max num of groups we can have is 30 */
	if (session->clist->num_groups == 30)
		return -1;
	
	encoded = url_encode (group_name);

	/* Check if group name is too long */
	if (strlen (encoded) > 61) {
		g_free (encoded);
		return -1;
	}

	ret = send_adg (session->notif->conn, encoded);

	g_free (encoded);

	return ret;
}

int 
msn_remove_group (MsnSession *session, int group_id)
{
	char *encoded = NULL;
	int ret = 0;

	g_return_val_if_fail (session != NULL, -1);
	g_return_val_if_fail (group_id >= 0, -1);
	
	ret = send_rmg (session->notif->conn, group_id);

	return ret;
}
